package com.ljx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.common.ResponseResult;
import com.ljx.domain.entity.Link;
import com.ljx.domain.vo.LinkVo;
import com.ljx.domain.vo.PageVo;
import com.ljx.mapper.LinkMapper;
import com.ljx.service.LinkService;
import com.ljx.utils.SystemConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 友链(SgLink)表服务实现类
 *
 * @author makejava
 * @since 2023-04-19 10:50:58
 */
@Service("LinkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public ResponseResult getAllLink() {
        LambdaQueryWrapper<Link> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> list = list(lambdaQueryWrapper);
        List<LinkVo> linkVos = list.stream().map(temp -> {
            LinkVo linkVo = new LinkVo();
            BeanUtils.copyProperties(temp, linkVo);
            return linkVo;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult listAll(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Link> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.hasText(name),Link::getName,name);
        lambdaQueryWrapper.eq(StringUtils.hasText(status),Link::getStatus,status);
        Page<Link> page = new Page<>(pageNum, pageSize);
        page(page,lambdaQueryWrapper);
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);
    }



}
